#include <iomanip>
#include "ItemsBrowser.h"

void ItemsBrowser::displayAItems(Stock &stock, string ID){
	cout << "Search Result: " << endl;
	int result = stock.setBufferItem(ID); //set buffer
	if(result == -1)
		cout << "---Item not found.---" << endl;
	else{
		cout << "The item you are looking for belongs to \"" << stock.getProductName(result) << "\"" << endl;
		displayItems(stock);
	}
}

void ItemsBrowser::displayATypeOfItems(Stock &stock, int type, int compareFactor){
	cout << stock.getProductName(type) << endl;
	if(stock.setBufferItem(type,compareFactor) == 0) //set buffer
		cout << "---No item is found---" << endl;
	else
		displayItems(stock);
}

void ItemsBrowser::displayATypeOfItems(Stock &stock, int type, int compareFactor, int threshold){
	cout << stock.getProductName(type) << endl;
	if(stock.setBufferItem(type,compareFactor, threshold) == 0) //set buffer
		cout << "---No item with shock lower than threshold is found---" << endl;
	else
		displayItems(stock);
}

void ItemsBrowser::displayAllItems(Stock &stock, int compareFactor){
	cout << "List of all items" << endl;

	for(int i = 0; i < 5; i++){
		//If it is not compared by ID (0), get the correct compareFactor
		int realFactor = compareFactor;
		if(compareFactor != 0){
			stock.setBufferItem(i);
			map <int, string> header = stock.getItemsHeader();
			realFactor = (int)header.size() - 4 + compareFactor;
		}
		cout << endl;
		displayATypeOfItems(stock, i, realFactor);
	}
}

void ItemsBrowser::displayAllItems(Stock &stock, int compareFactor, int threshold){
	cout << "List of all items with shock below " << threshold << ":" << endl;

	for(int i = 0; i < 5; i++){
		//If it is not compared by ID (0), get the correct compareFactor
		int realFactor = compareFactor;
		if(compareFactor != 0){
			stock.setBufferItem(i);
			map <int, string> header = stock.getItemsHeader();
			realFactor = (int)header.size() - 4 + compareFactor;
		}
		cout << endl;
		displayATypeOfItems(stock, i, realFactor, threshold);
	}
}

void ItemsBrowser::displaySLine(int length){
	for(int i = 0; i < length; i++)
			cout << "-";
	cout << endl;
}

void ItemsBrowser::displayItems(Stock &stock){
	//Get all necessary information and data from stock
	map <int, string> header = stock.getItemsHeader();
	map <int, int> width = stock.getItemsHeaderLength();
	map <int, string> data = stock.getItemsDetails();

	//Calculate table size
	int noOfCol = header.size();
	int noOfRow = data.size()/noOfCol;
	int lineLength = 1;

	for(int i = 0; i < noOfCol; i++)
		lineLength += width[i] + 2;

	displaySLine(lineLength);

	//Display header of table
	for(int i = 0; i < noOfCol; i++)
		cout << "| " << left << setw(width[i]) << header[i];
	cout << "|" << endl;

	displaySLine(lineLength);

	//Display content of table
	for(int i = 0; i < noOfRow; i++){
		for(int j = 0; j < noOfCol; j++)
			cout << "| " << left << setw(width[j]) << data[i*noOfCol + j];
		cout << "|" << endl;
	}
	displaySLine(lineLength);
}